﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class SMTP
    {
        public static string SendCheckCodeToEmail()
        {
            string responseMessage = "";
            Random rd = new Random(DateTime.Now.Millisecond);
            string CreateCheckCode = rd.Next(1000, 9999).ToString();
            string mailServerName = "smtp.qq.com";
            string MailFrom = System.Configuration.ConfigurationSettings.AppSettings["UserName"];
            string pwd = "xhjdnpqlclsleaaa";
            string mailTo = "52727246@qq.com";
            string subject = "SMTP-test";
            StringBuilder body = new StringBuilder();
            body.Append("您好！验证码为：  " + CreateCheckCode);
            body.Append("<br/>");
            body.Append("<br/>张龙服务中心有限公司");
            using (MailMessage message = new MailMessage(MailFrom, mailTo, subject, body.ToString()))
            {

                try
                {
                    message.IsBodyHtml = true;
                    //SmtpClient是发送邮件的主体，这个构造函数是告知SmtpClient发送邮件时使用哪个SMTP服务器
                    SmtpClient mailClient = new SmtpClient(mailServerName);
                    //将认证实例赋予mailClient,也就是访问SMTP服务器的用户名和密码
                    mailClient.Credentials = new NetworkCredential(MailFrom, pwd);
                    //最终的发送方法
                    mailClient.Send(message);
                    //邮件发送成功
                    responseMessage = "发送成功";
                }
                catch (Exception ex)
                {
                    //邮件发送失败
                    responseMessage = "发送失败";
                }
            }
            return responseMessage;
        }

    }
}
